
#import "ConversionViewController.h"
#import "HelpViewController.h"
#import "Data.h"
#import "CustomNavigationBar.h"
#import "UINavigationItem+Customization.h"

@implementation ConversionViewController

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-off.png"] forState:UIControlStateNormal];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateHighlighted];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateSelected];
    [infoButton sizeToFit];
    [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
    
    CustomNavigationBar *navbar = (CustomNavigationBar *)self.navigationController.navigationBar;
    [navbar applyDefaultStyle];
    
    UIBarButtonItem *infoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    infoBarButtonItem.accessibilityLabel = @"Hjälp";
    self.navigationItem.rightBarButtonItem = infoBarButtonItem;
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [self showConversionDocument];
     self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem customizeTitleInNavigationBarWithHeight:self.navigationController.navigationBar.frame.size.height];
}

#define kLampType @"lampType"
#define kPlaceholder @"placeholder"
-(void)showConversionDocument
{
    NSString *documentName = @"Konverteringstabell";
    NSMutableString *html = [[self loadDocumentWithName:documentName] mutableCopy];
    
    Data *data = [Data sharedInstance];
    NSArray *parts = @[@{kLampType: @"classic", kPlaceholder: @"$conversion_table$"},
                       @{kLampType: @"halogen_gu53", kPlaceholder: @"$conversion_table_r12$"},
                       @{kLampType: @"halogen_gu10", kPlaceholder: @"$conversion_table_r230$"}
                       ];
    [parts enumerateObjectsUsingBlock:^(NSDictionary *part, NSUInteger idx, BOOL *stop) {
        NSDictionary *wattToLumenTable = [data wattToLumenTableForLampType:part[kLampType]];
        NSArray *sortedKeys = [wattToLumenTable.allKeys sortedArrayUsingComparator:^(NSString *s1, NSString *s2) {
            return [s1 compare:s2 options:NSNumericSearch];
        }];
                               
        NSMutableString *table = [NSMutableString string];
        for (NSNumber *key in sortedKeys) {
            NSString *interval = [[Data sharedInstance] lumenStringFromWattValue:[key integerValue] lampType:part[kLampType]];
            [table appendFormat:@"<tr><td><div>%d&nbsp;W</div></td><td><div>%@&nbsp;lm</div></td></tr>", [key integerValue], interval];
        }
        [html replaceOccurrencesOfString:part[kPlaceholder] withString:table options:0 range:NSMakeRange(0, html.length)];
    }];
    
//    NSLog(@"%@", html);
    [self showDocumentWithName:documentName andHtml:html];
}

- (void)showInfo
{
    HelpViewController *helpVC = [[HelpViewController alloc] initWithNibName:nil bundle:nil];
    helpVC.documentFile = _(@"Conversion");
    helpVC.title = _(@"Hjälp");
    [self presentModalViewController:helpVC animated:YES];
}

@end
